﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using ChBusiness.Wsbs;
using ChBusiness.Common;
using ChBusiness.User;
using ChBusiness;
using ChBusiness.FlowOperation;
using ChBusiness.Menu;

public partial class Default : System.Web.UI.Page
{
    private WebWork cWorks = new WebWork();
    private SerialInstanceOperation instanceOperation = new SerialInstanceOperation();
    private SerialInstance cInstance = new SerialInstance();
    private string strShouJian = string.Empty; //累计收件个数
    private string strBanJie = string.Empty; //累计办结个数

    #region 页面加载事件
    /// <summary>
    /// Handles the Load event of the Page control.
    /// </summary>
    /// <param name="sender">The source of the event.</param>
    /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
    protected void Page_Load(object sender, EventArgs e)
    {
        //跳转到静态页面
        if (SystemConfig.LoginPage.ToLower() != "default.aspx")
        {
            Response.Redirect(SystemConfig.LoginPage);
        }

        if (!IsPostBack)
        {
            Session["SelectSys"] = SystemConfig.WebSystemFlag;
            Session["MainPage"] = Request.ApplicationPath + "/"+SystemConfig.LoginPage;

            if (!string.IsNullOrEmpty(Request.Params["UserID"]))
            {
                this.UserName.Text = Request.Params["UserID"];
            }        
            BindXzxkjgData();
            msgXxgg.MsgDataType = "信息公告";
            BindFlowName();
            ViewState["strShouJian"] = cInstance.GetShoujianTotal();//累计收件个数
            ViewState["strBanJie"] = cInstance.GetBanjieTotal(); //累计办结个数
        }
    }
    #endregion

    #region 行政许可结果公示
    /// <summary>
    /// Binds the data.
    /// </summary>
    private void BindXzxkjgData()
    {
        DataTable dtSource = instanceOperation.GetInstanceInfo("","","","","");

        this.dgXkjggs.DataSource = dtSource;
        this.dgXkjggs.PageSize = SystemConfig.PageSize;
        this.dgXkjggs.RecordCount = dtSource.Rows.Count;
        this.dgXkjggs.DataBind();
    }

    /// <summary>
    /// 许可结果公示的翻页事件
    /// </summary>
    /// <param name="sender">The source of the event.</param>
    /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
    protected void dgXkjggs_OnLoadData(object sender, EventArgs e)
    {
        BindXzxkjgData();
    }
    #endregion
    
    #region 系统登录
    /// <summary>
    /// 
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void LoginButton_Click(object sender, EventArgs e)
    {
        if (Session["CheckCode"] == null)
        {
            return;
        }

        if (txtCheckCode.Text.ToUpper() != Session["CheckCode"].ToString())　　//验证码
        {
            labMsg.Text = "验证码错误！";
            return;
        }

        string userName = UserName.Text;//UserName.Text.ToUpper();不再将用户名转为大小之后进行验证

        bool userExist = true;

        //验证是否是管理员
        if (SystemConfig.AdminUser.CompareTo(userName) != 0)
        {
            userExist = UserHandle.CheckUserExist(userName, Session["SelectSys"].ToString());

            if (!userExist)
            {
                labMsg.Text = "用户名不存在！";
                return;
            }
            else
            {
                if (!UserHandle.UserIsvalid(userName))
                {
                    labMsg.Text = "该用户还未通过审核！";
                    return;
                }
                else//用户验证成功
                {
                    Session["title"] = "国家测绘地理信息局业务申报系统";

                    Session.Remove("Menu");

                    //对预审标志初始化
                    Session["PromiseFlag"] = string.Empty;
                    Session["UserID"] = UserName.Text; //Login1.UserName.ToString();
                    Session["LoginUserName"] = UserHandle.GetNotes(UserName.Text);
                    RecordLog(userName);

                    Response.Redirect("Mainpage.htm");
                }
            }
        }
        else
        {
            Session["title"] = "国家测绘地理信息局业务申报系统后台维护";
        }
    }

    #endregion

    #region 记录登录日志
    /// <summary>
    /// 记录登录日志
    /// </summary>
    /// <param name="UserName"></param>
    private void RecordLog(string UserName)
    {
        string UserIP = Request.UserHostName.ToString();
        SystemLogs wyLog = new SystemLogs();
        wyLog.Inputlog(UserName, "登录外网申报系统", string.Empty, UserIP);
    }
    #endregion 

    #region 行政许可事项查看
    /// <summary>
    /// 获取行政许可事项查看
    /// </summary>
    /// <param name="row">The row.</param>
    /// <returns></returns>
    public string BindXzxk()
    {
        string strHtml = string.Empty;
        DataTable dtTable = cWorks.GetWorkFlow();
        //获取菜单
        ClsMenuOperation menuOperation = new ClsMenuOperation();
        DataTable dtMenu = menuOperation.GetChildMenus("网上办事");

        foreach (DataRow drOne in dtTable.Rows)
        {
            string strFlowName = drOne["FLOWNAME"].ToString();
            string strFlowType = drOne["FLOWTYPE"].ToString();//流程别名 addby zhongjian 20091104
            string FlowName = Server.UrlEncode(strFlowName);
            string FlowType = Server.UrlEncode(strFlowType);
            //添加单个办理事项的连接 addby zhongjian 20100111
            strHtml += string.Format(@"<table class='page_c_box' border='0' cellspacing='6' style='margin-top: 2px;'>
                                <tr><td colspan='8'><div class='page_bar_02'>
                                            <a href='Wsbs/IndexPage.aspx?&flowname={1}&flowtype={2}&type=0' target='_blank'>{0}</a></div></td></tr>", strFlowType, FlowName, FlowType);

            //获取菜单权限
            DataTable dtMenuRight = cWorks.GetChildMenuRight(strFlowName);

            string strTr = string.Empty;
            foreach (DataRow drTemp in dtMenu.Rows)
            {
                string strMenuTitle = drTemp["menu_title"].ToString();
                string strMenuUrl = drTemp["menu_url"].ToString();

                dtMenuRight.DefaultView.RowFilter = string.Format("menu_name='{0}'", strMenuTitle);

                if (dtMenuRight.DefaultView.Count > 0)
                {
                    if (dtMenuRight.DefaultView[0].Row["menu_type"].ToString() == "1")
                    {
                        strTr += string.Format("<td ><a href='{0}&flowname={1}&flowtype={3}' target=\"_blank\"><font color=\"black\">{2}</a></font></td>",
                            strMenuUrl.Replace("~/", "").Replace("WsbsMain", "Wsbs"), FlowName, strMenuTitle, FlowType);
                    }
                    else
                    {
                        strTr += string.Format("<td ><font color=\"gray\">{0}</font></td>", strMenuTitle);
                    }
                }
            }

            strHtml += string.Format(@"<tr style='height: 40px;'>{0}</tr></table>", strTr);
        }
        return strHtml;
    }
    #endregion

    #region 办件统计模块
    /// <summary>
    /// 累计收件个数
    /// </summary>
    /// <returns></returns>
    protected string GetShoujianCount()
    {
        return ViewState["strShouJian"].ToString();
    }
   
    /// <summary>
    /// 累计办结个数
    /// </summary>
    /// <returns></returns>
    protected string GetBanjieCount()
    {
        return ViewState["strBanJie"].ToString();
    }
    #endregion

    #region 绑定通知通报、绑定常见问题、绑定留言咨询   
    /// <summary>
    /// 绑定常见问题
    /// </summary>
    /// <!--addby zhongjian 20091028-->
    public string BindQuestion()
    {
        DataTable dtSource = instanceOperation.GetAskQuestion("", "常见问题", "", 4, 13);
        string strHtml = string.Empty;
        foreach (DataRow drTemp in dtSource.Rows)
        {
            strHtml += string.Format(@"<li><a href='Wsbs/detail.aspx?id={0}&type=1' target='_blank'>{1}</a></li>", drTemp["id"], drTemp["title"]);
        }
        return strHtml;       
    }

    /// <summary>
    /// 绑定留言咨询
    /// </summary>
    /// <!--addby zhongjian 20091028-->
    public string BindLyzx()
    {
        DataTable dtSource = instanceOperation.GetLyzxInfo("", true, 4, 13,"","","","","");
        string strHtml = string.Empty;
        foreach (DataRow drTemp in dtSource.Rows)
        {
            strHtml += string.Format(@"<li><a href='Wsbs/LyzxMessage.aspx?id={0}' target='_blank'>{1}</a></li>", drTemp["id"], drTemp["title"]);
        }
        return strHtml;       
    }
    #endregion

    /// <summary>
    /// 绑定所有流程名称
    /// </summary>
    /// <!--addby zhongjian 20091225-->
    public void BindFlowName()
    {
        UserRightRequest urrTemp = new UserRightRequest();
        DataTable dtTable = urrTemp.GetFlowList();
        ddlFlow.Items.Clear();
        ddlFlow.Items.Add(new ListItem("请选择事项流程...", "0"));
        foreach (DataRow row in dtTable.Rows)
        {
            ddlFlow.Items.Add(new ListItem(row["KEYVALUE"].ToString(), row["KEYVALUE"].ToString()));
        }
    }
}
